package org.jeecg.modules.demo.yxmodalshift.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soOrderDetail.mapper.SoOrderDetailMapper;
import org.jeecg.modules.demo.utils.BigDecimalUtil;
import org.jeecg.modules.demo.utils.CreateCodeUtil;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.mapper.WarehouseOrderMapper;
import org.jeecg.modules.demo.yxmodalshift.entity.YxModalshift;
import org.jeecg.modules.demo.yxmodalshift.mapper.YxModalshiftMapper;
import org.jeecg.modules.demo.yxmodalshift.service.IYxModalshiftService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 形态转换单
 * @Author: jeecg-boot
 * @Date:   2022-07-15
 * @Version: V1.0
 */
@Service
public class YxModalshiftServiceImpl extends ServiceImpl<YxModalshiftMapper, YxModalshift> implements IYxModalshiftService {
    @Resource
    private YxModalshiftMapper yxModalshiftMapper;
    @Resource
    private WarehouseOrderMapper warehouseOrderMapper;
    @Resource
    private SoOrderDetailMapper soOrderDetailMapper;

    @Override
    public String getMaxCode(){
        return yxModalshiftMapper.getMaxCode();
    }

    @Override
    @Transactional
    public boolean checkModalShift(YxModalshift yxModalshift,String precode){
        try{
            yxModalshift.setShenhe(1);
            yxModalshift.setCheckTime(new Date());
            WarehouseOrder warehouseOrder = warehouseOrderMapper.getByWoNo(yxModalshift.getWoNo());
            warehouseOrder.setZhzt(2);
            //warehouseOrder.setShenhe(1);
            warehouseOrder.setStandingstock(new BigDecimal(BigDecimalUtil.sub(warehouseOrder.getStandingstock().doubleValue(),yxModalshift.getZhsl().doubleValue())));
            String rkno = getRkCode(precode);
            WarehouseOrder order = createWareHouseOrder(yxModalshift,warehouseOrder,rkno);
            String ckno = getCKCode(precode);
            SoOrderDetail orderDetail = createSoOrderDetail(yxModalshift,warehouseOrder,ckno);
            yxModalshift.setQtckno(ckno);
            yxModalshift.setQtrkno(rkno);
            yxModalshiftMapper.updateById(yxModalshift);
            warehouseOrderMapper.updateById(warehouseOrder);
            warehouseOrderMapper.insert(order);
            soOrderDetailMapper.insert(orderDetail);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional
    public boolean unCheckModalShift(YxModalshift yxModalshift){
        try{
            WarehouseOrder warehouseOrder = warehouseOrderMapper.getByWoNo(yxModalshift.getWoNo());
            warehouseOrder.setZhzt(1);
            warehouseOrder.setStandingstock(new BigDecimal(BigDecimalUtil.add(yxModalshift.getZhsl().doubleValue(),warehouseOrder.getStandingstock().doubleValue())));
            WarehouseOrder order  = warehouseOrderMapper.getByWoNo(yxModalshift.getQtrkno());
            warehouseOrderMapper.deleteById(order.getId());
            SoOrderDetail orderDetail = soOrderDetailMapper.getBySodNumber(yxModalshift.getQtckno());
            soOrderDetailMapper.deleteById(orderDetail.getId());
            yxModalshift.setQtrkno(null);
            yxModalshift.setQtckno(null);
            yxModalshift.setCheckTime(null);
            yxModalshift.setShenhe(0);
            warehouseOrderMapper.updateById(warehouseOrder);
            yxModalshiftMapper.updateById(yxModalshift);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    /**
     * 其他入库单号bug解决
     * @return
     */
    public String getRkCode(String precode){
        String str = warehouseOrderMapper.getMaxCode();
        Map<String,String> map = new HashMap<>();
        if(str==null){
            map.put("maxCode",null);
        }else{
            map.put("maxCode",str);
        }
        String name = precode+"QT_";
        String resultCode = generateOddUtil.createCode(map,name);
        return resultCode;
    }

    /**
     * 其他出库单号bug解决
     * @return
     */
    public String getCKCode(String precode){
        String str = soOrderDetailMapper.getMaxCode();
        Map<String,String> map = new HashMap<>();
        if(str==null){
            map.put("maxCode",null);
        }else{
            map.put("maxCode",str);
        }
        String name = precode+"QT_";
        String resultCode = generateOddUtil.createCode(map,name);
        return resultCode;
    }

    /**
     * 生成其他出库单
     * @param orderRk
     * @return
     */
    public SoOrderDetail createSoOrderDetail(YxModalshift modalshift,WarehouseOrder orderRk, String str){
        SoOrderDetail soOrderDetail = new SoOrderDetail();
        soOrderDetail.setSodNo(str);
        soOrderDetail.setSodQuantity(modalshift.getZhsl());
        soOrderDetail.setXclNum(modalshift.getZhsl());
        soOrderDetail.setIsQuality(0);
        soOrderDetail.setSodDate(new Date());
        soOrderDetail.setCreateTime(new Date());
        soOrderDetail.setWhNum(orderRk.getWhNum());
        soOrderDetail.setPuWhNum(orderRk.getWhNum());
        //生成其他出库单直接审核
        //soOrderDetail.setSodStatus(0);
        soOrderDetail.setSodStatus(1);
        //赋值同部编号
        soOrderDetail.setTradecode(orderRk.getTradecode());
        soOrderDetail.setIsDelete(0);
        soOrderDetail.setPrdNo(orderRk.getCinvname());
        soOrderDetail.setJldw(orderRk.getMeasureunit());
        soOrderDetail.setPrice(orderRk.getFthsdj().doubleValue());
        soOrderDetail.setWsdj(orderRk.getFtwsdj());
        soOrderDetail.setWsje(new BigDecimal(BigDecimalUtil.mul(modalshift.getZhsl().doubleValue(),orderRk.getFtwsdj().doubleValue())));
        soOrderDetail.setHsdj(orderRk.getFthsdj());
        soOrderDetail.setHsje(new BigDecimal(BigDecimalUtil.mul(modalshift.getZhsl().doubleValue(),orderRk.getFthsdj().doubleValue())));
        soOrderDetail.setCkType("3");
        soOrderDetail.setSoStaff(modalshift.getPoStaff());
        soOrderDetail.setDept(modalshift.getDepartment());
        soOrderDetail.setCbJine(new BigDecimal(BigDecimalUtil.mul(modalshift.getZhsl().doubleValue(),orderRk.getFtwsdj().doubleValue())));
        soOrderDetail.setShName(modalshift.getSaleman());
        soOrderDetail.setRkNo(modalshift.getWoNo());
        return soOrderDetail;
    }

    /**
     * 生成其他入库单
     * @param orderRk
     * @return
     */
    public WarehouseOrder createWareHouseOrder(YxModalshift modalshift, WarehouseOrder orderRk, String str){
        WarehouseOrder warehouseOrder = new WarehouseOrder();
        warehouseOrder.setWoNo(str);
//        warehouseOrder.setPuWoNo(str);
        warehouseOrder.setPuWoNo(orderRk.getWoNo());

        warehouseOrder.setProWhNum(orderRk.getWhNum());
        warehouseOrder.setWhNum(orderRk.getWhNum());
        warehouseOrder.setQoQuantity(modalshift.getZhsl());
        warehouseOrder.setWoDate(new Date());
        warehouseOrder.setCreateTime(new Date());
        warehouseOrder.setPuWhNum(orderRk.getWhNum());
        //形态转换单直接生成已审核的入库单
        warehouseOrder.setShenhe(1);
        //warehouseOrder.setShenhe(0);
        //赋值tradercode
        warehouseOrder.setTradecode(orderRk.getTradecode());
        warehouseOrder.setShanchu(0);
        warehouseOrder.setStandingstock(modalshift.getZhsl());
        warehouseOrder.setMeasureunit(orderRk.getMeasureunit());
        warehouseOrder.setDepartment(modalshift.getDepartment());
        warehouseOrder.setPoStaff(modalshift.getPoStaff());
        warehouseOrder.setRklb(3);
        warehouseOrder.setCinvname(modalshift.getZhcocalname());
        warehouseOrder.setPdNo(orderRk.getPdNo());
        warehouseOrder.setSaleman(modalshift.getSaleman());
        warehouseOrder.setWsdj(orderRk.getFtwsdj());
        warehouseOrder.setHsdj(orderRk.getFthsdj());
        warehouseOrder.setWsje(new BigDecimal(BigDecimalUtil.mul(modalshift.getZhsl().doubleValue(),orderRk.getFtwsdj().doubleValue())));
        warehouseOrder.setHsje(new BigDecimal(BigDecimalUtil.mul(modalshift.getZhsl().doubleValue(),orderRk.getFthsdj().doubleValue())));
        warehouseOrder.setFtwsdj(warehouseOrder.getWsdj());
        warehouseOrder.setFthsdj(warehouseOrder.getHsdj());
        warehouseOrder.setFthsje(warehouseOrder.getHsje());
        warehouseOrder.setFtwsje(warehouseOrder.getWsje());
        warehouseOrder.setZhzt(0);
        warehouseOrder.setCreateBy(modalshift.getCreateBy());
        warehouseOrder.setSysOrgCode(modalshift.getSysOrgCode());
        warehouseOrder.setPoOrder(orderRk.getPoOrder());
        String code = warehouseOrderMapper.getCode(warehouseOrder.getPoOrder());
        Map<String,String> mapCode = new HashMap<>();
        if(code==null){
            mapCode.put("maxCode",null);
        }else{
            mapCode.put("maxCode",code);
        }
        String prdbn = CreateCodeUtil.createCode(mapCode);
        warehouseOrder.setPrdBn(prdbn);
        warehouseOrder.setChumdwmc(orderRk.getChumdwmc());
        return warehouseOrder;
    }
}
